//
//  GWEmotionToolBar.m
//  新浪微博
//
//  Created by qianfeng on 16/2/20.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWEmotionToolBar.h"

#define ToolViewMaxBtnCount 4

@interface GWEmotionToolBar ()

@property (nonatomic,weak)UIButton *selectedBtn;
@end
@implementation GWEmotionToolBar

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        //3.部署btn
        [self setupBtnWithTitle:@"最近" type:GWEmotionTypeRecent];
        [self setupBtnWithTitle:@"默认" type:GWEmotionTypeDefault];
        [self setupBtnWithTitle:@"Emoji" type:GWEmotionTypeEmoji];
        [self setupBtnWithTitle:@"浪小花" type:GWEmotionTypeLxh];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didSelectedEmotion:) name:GWEmotionDidSelectedNotification object:nil];
    }
    return self;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)didSelectedEmotion:(NSNotification *)note
{
    if (self.selectedBtn.tag ==GWEmotionTypeRecent) {
        [self toolBtnOnClick:self.selectedBtn];
    }
}
-(UIButton *)setupBtnWithTitle:(NSString *)title type:(GWEmotionType)type
{
    UIButton *btn =[[UIButton alloc]init];
    btn.tag =type;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
    btn.titleLabel.font =[UIFont systemFontOfSize:13];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(toolBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn];
    
    if (self.subviews.count==1) {
        [btn setBackgroundImage:[UIImage reSizeWithName:@"compose_emotion_table_left_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage reSizeWithName:@"compose_emotion_table_left_selected"] forState:UIControlStateSelected];
    }
    else if (self.subviews.count==ToolViewMaxBtnCount)
    {
        [btn setBackgroundImage:[UIImage reSizeWithName:@"compose_emotion_table_right_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage reSizeWithName:@"compose_emotion_table_right_selected"] forState:UIControlStateSelected];
    }
    else
    {
        [btn setBackgroundImage:[UIImage reSizeWithName:@"compose_emotion_table_mid_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage reSizeWithName:@"compose_emotion_table_mid_selected"] forState:UIControlStateSelected];
    }
    return btn;
}
-(void)toolBtnOnClick:(UIButton *)btn
{
    self.selectedBtn.selected =NO;
    btn.selected =YES;
    self.selectedBtn =btn;
    
    if ([self.delegate respondsToSelector:@selector(emotionToolBar:didSelectedBtn:)]) {
        [self.delegate emotionToolBar:self didSelectedBtn:(GWEmotionType)btn.tag];
    }
}
-(void)setDelegate:(id<GWEmotionToolBarDelegate>)delegate
{
    _delegate =delegate;
    UIButton *defaultBtn =(UIButton *)[self viewWithTag:GWEmotionTypeDefault];
    [self toolBtnOnClick:defaultBtn];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnW =self.width/ToolViewMaxBtnCount;
    CGFloat btnH =self.height;
    for (int i=0; i<ToolViewMaxBtnCount; i++) {
        UIButton *btn =self.subviews[i];
        btn.frame =CGRectMake(i*btnW, 0, btnW, btnH);
    }
}
@end
